<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>游戏登入页面</title>
		<style>
		body{background:url(img/background.jpg);
			display: flex;
			/*弹性盒子*/
			margin-top: 300px;
			margin-left: 1000px;
		background-size:100%;}
		</style>
		<script src="js/jquery.min.js"></script>
		<script>
			var codekey;
			$(function (){
				codekey=Math.random()*10000;
				createcode();
			});
			function goRegisterPage() {
				location.href = "register.html"
			}
			function forgetPassword(){
				location.href="forgetPassword.html"
			}

			function createcode(){
				$("#codeimg").attr("src","http://localhost:8080/user/code?codekey="+codekey+"&time="+new Date().getTime());
			}

				function login() {
					var username = $("#username").val();
					var password = $("#password").val();
					var code = $("#code").val();
					if (username == null || username.trim() == "") {
						alert("账号不能为空");
						return;
					}
					if (password == null || password.trim() == "") {
						alert("密码不能为空");
						return;
					}



					$.ajax({
						type: "post",
						url: "http://localhost:8080/user/login",
						data: {
							username: username,
							password: password,
							codekey: codekey,
							code: code
						},
						success: function(data){
							//登录结果的反馈
							//JSON.stringify - JS的对象 转换成为字符串
							//js - 非null的对象，默认为true，否则就是false
							if(data){
								if(data.username=="-1"){
									alert("验证码错误");
									return;
								}
								//登录成功
								alert("登录成功！");
								localStorage.setItem("username", data.username);
								localStorage.setItem("nickname", data.nickname);
								localStorage.setItem("header", data.header);
								localStorage.setItem("bean",data.bean);
								localStorage.setItem("uid",data.id);
								location.href = "begin.html"
							} else {
								alert("登入失败");
							}
						}
					});
				}
		</script>
	</head>
	<body >
		<div >
			<h1>登录游戏</h1>
				<div>
					账号:<br><input  id=username type="text" placeholder="输入账号">
				</div>
				<div>
					密码:<br><input  id=password type="password" placeholder="输入密码">
				</div>
				<div>
					验证码:
					<br><input  id=code>
					<br><img id="codeimg"
						src="http://localhost:8080/user/code"
							 onclick="createcode()"
							 style="background-color: #888888; width:80px; height:40px;" />
				</div>
				<div>
					<button onclick="login()">登陆</button>
					<button onclick="goRegisterPage()">注册</button>
					<button onclick="forgetPassword()">忘记密码</button>
				</div>
		</div>
	</body>
</html>
